package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Sound;
import javafx.scene.shape.Circle;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.expression.spel.ast.NullLiteral;

import javax.swing.*;
import java.util.List;

@DubboService
public class PeachblossomApiImpl implements PeachblossomApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    //接收
    public Sound receive(Long listenId,String date) {
        Criteria criteria = Criteria.where("userId").nin(listenId).and("hasRead").is(0);
        Query query = Query.query(criteria);
        Sound sound = mongoTemplate.findOne(query, Sound.class);
        //修改这条属性
        Update update = new Update();
        update.set("listenId",listenId);
        update.set("hasRead",1);
        update.set("date",date);
        mongoTemplate.updateFirst(query,update,Sound.class);
        return sound;
    }

    //保存
    public void save(Sound sound) {
        mongoTemplate.save(sound);
    }

    //查询语音发送的次数
    public Integer find(Long listenId, String date) {
        Criteria criteria = Criteria.where("listenId").is(listenId).and("date").is(date);
        Query query = Query.query(criteria);
        int count = (int) mongoTemplate.count(query, Sound.class);

        return count;
    }
}
